<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
<script type="text/javascript">
    var book = {
        name: 'Catch-22',
        published: 1961,
        author: {
            firstname: 'Joseph',
            lastname: 'Heller'
        }
    }

    // 使用点号来访问
    console.log(book.author.firstname);

    // 使用中括号来访问
    console.log(book['author']['lastname']);

    // 混合使用来访问
    console.log(book.author['lastname']);
    console.log(book['author'].lastname);

    // 访问不确定属性名时可以定义变量来访问
    var key = 'firstname';
    console.log(book.author[key]);
</script>

<!-- P100 -->
<script type="text/javascript">
    var hero = {
        breed: 'Turtle',
        occupation: 'Ninja',
        say: function() {
            return 'I am ' + hero.occupation;
        }
    }

    console.log(hero.say());

    // 还可以传参数
    console.log(hero.say('a'));

    // 其他调用方式 - 尽量少使用中括号来引用
    console.log(hero['say']());
</script>
</body>
</html>
